package com.xinshujia.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.google.gson.Gson;
import com.xinshujia.entity.DeviceEntity;
import com.xinshujia.response.RespResult;
import com.xinshujia.service.DeviceService;
import com.xinshujia.utils.GsonUtil;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * *************************************
 * *   @author: 野狼上校               **
 * *   @since: 2024-08-06 15:20       **
 * *   @version: 1.0                  **
 * *************************************
 * <p>
 * 设备控制类
 */
@RestController
@RequestMapping("device")
public class DeviceController {
    private final DeviceService deviceService;

    public DeviceController(DeviceService deviceService) {
        this.deviceService = deviceService;
    }

    /**
     * 分页查询
     */
    @RequestMapping("getDevicesByPage")
    public RespResult getDevicesByPage(@RequestParam("pageNum") Long pageNum,
                                       @RequestParam("pageSize") Long pageSize,
                                       @RequestParam("companyName") String companyName) {
        Page<DeviceEntity> pageDevices = deviceService.getDevicesByPage(pageNum, pageSize, companyName);
        Gson gson = GsonUtil.googleGson();
        return RespResult.OK(gson.toJson(pageDevices));
    }

    /**
     * 添加设备
     */
    @PostMapping("insertDevice")
    public RespResult insertDevice(@RequestBody DeviceEntity deviceEntity) {
        boolean flag = deviceService.insertDevice(deviceEntity);
        return flag ? RespResult.OK() : RespResult.Fail();
    }

    /**
     * 修改
     */
    @PostMapping("updateDevice")
    public RespResult updateDevice(@RequestBody DeviceEntity deviceEntity) {
        boolean flag = deviceService.updateById(deviceEntity);
        return flag ? RespResult.OK() : RespResult.Fail();
    }

    /**
     * 所有的设备列表
     */
    @GetMapping("getAllDevices")
    public RespResult getAllDevices(){
        List<DeviceEntity> list = deviceService.getAllDevices();
        Gson gson = GsonUtil.googleGson();
        return RespResult.OK(gson.toJson(list));

    }

}
